<?php
declare (strict_types = 1);

namespace kernel;
use Inhere\Route\Router;
use kernel\Config;
use think\event\AppInit;

/**
 *  * @property \kernel\Config $config
 */
class App extends Container
{

    /**
     * 当前应用类库命名空间
     * @var string
     */
    protected $namespace = 'kernel';

    /**
     * 应用根目录
     * @var string
     */
    protected $rootPath = '';

    /**
     * 框架目录
     * @var string
     */
    protected $corePath = '';

    /**
     * 应用目录
     * @var string
     */
    protected $appPath = '';

    /**
     * Runtime目录
     * @var string
     */
    protected $runtimePath = '';

    /**
     * 配置后缀
     * @var string
     */
    protected $configExt = '.php';

    /**
     * 路由
     * @var
     */
    protected $route;


    /**
     * 容器绑定标识
     * @var array
     */
    protected $bind = [
        'app'                     => \kernel\App::class,
        'config'                  => Config::class,

    ];

    /**
     * @param string $rootPath
     */
    public function __construct(string $rootPath = '')
    {

        $this->corePath   = realpath(dirname(__DIR__)) . DIRECTORY_SEPARATOR;
        $this->rootPath    = $rootPath ? rtrim($rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $this->getDefaultRootPath();
        $this->appPath     = $this->rootPath . 'app' . DIRECTORY_SEPARATOR;
        $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR;

        static::setInstance($this);

        $this->instance('app', $this);
        $this->instance('kernel\Container', $this);

        $this->initialize();

    }

    /**
     * 初始化应用
     * @access public
     * @return \think\App
     */
    public function initialize()
    {
        //$this->initialized = true;


        // 加载全局初始化文件
        $this->load();

        date_default_timezone_set($this->config->get('app.default_timezone', 'Asia/Shanghai'));


        return $this;
    }

    /**
     * 加载应用文件和配置
     * @access protected
     * @return void
     */
    protected function load(): void
    {
        $configPath = $this->getConfigPath();

        $files = [];

        if (is_dir($configPath)) {
            $files = glob($configPath . '*' . $this->configExt);
        }
        foreach ($files as $file) {
            $this->config->load($file, pathinfo($file, PATHINFO_FILENAME));
        }


    }


    public function run(){
        //启动路由引擎
        $router = new Router();
        $routeConfig = require '../config/route.php';
        $router->config($routeConfig);

        $this->route = $router;

        //加载自定义路由列表
        require_once '../route/route.php';

        $this->route->dispatch();
    }

    /**
     * 获取应用根目录
     * @access protected
     * @return string
     */
    protected function getDefaultRootPath(): string
    {
        return dirname($this->corePath, 4) . DIRECTORY_SEPARATOR;
    }

    /**
     * 获取应用配置目录
     * @access public
     * @return string
     */
    public function getConfigPath(): string
    {
        return $this->rootPath . 'config' . DIRECTORY_SEPARATOR;
    }

    /**
     * 获取配置后缀
     * @access public
     * @return string
     */
    public function getConfigExt(): string
    {
        return $this->configExt;
    }


}